home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / contrib / dvx / demos / muncher / muncher.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-15  |  6.1 KB  |  234 lines

  1. /* 
  2.  * $Locker:  $ 
  3.  */ 
  4. static char    *rcsid = "$XConsortium: muncher.c,v 1.9 89/12/14 09:53:35 rws Exp $";
  5. /******************************************************************************
  6.  * Description:
  7.  *    The famous munching squares.
  8.  *
  9.  * Brought to you by Jef Poskanzer.
  10.  *
  11.  * Copyright (C) 1987 by UniSoft Systems.  Permission to use, copy,
  12.  * modify, and distribute this software and its documentation for any
  13.  * purpose and without fee is hereby granted, provided that this copyright
  14.  * notice appear in all copies and in all supporting documentation.  No
  15.  * representation is made about the suitability of this software for any
  16.  * purpose.  It is provided "as is" without express or implied warranty.
  17.  *
  18.  * Arguments:
  19.  *    -r        display on root window instead of creating a new one
  20.  *    -s seed        use this for the seed
  21.  *    =wxh+x+y    X geometry for new window (default 256x256 centered)
  22.  *    host:display    X display on which to run
  23.  *****************************************************************************/
  24.  
  25.  
  26. #if defined (PROTO) || defined(PROTO1) /* JDC 91/04/22 */
  27. #define NeedFunctionPrototypes 0
  28. #define NeedFunctionPtrPrototypes 1
  29. #include "muncher.fd1"
  30. #endif
  31.  
  32. #if PROTO
  33. #include "muncher.fd2"
  34. #endif
  35.  
  36. #include <X11/Xlib.h>
  37. #include <X11/Xatom.h>
  38. #include <X11/Xutil.h>
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <time.h> /* JDC 91/08/08 */
  42. char *ProgramName;
  43.  
  44. extern GC XCreateGC();
  45.  
  46. #ifdef METAWARE_V3       /* MEA 92/7/29 */
  47. #define random rand
  48. #define srandom srand
  49. #elif defined(SYSV) || defined(MSDOS) /* 90/06/26 POHC */
  50. extern int rand();
  51. #else
  52. extern long time();
  53. extern long random();
  54. #endif
  55.  
  56. Bool verbose = False;
  57.  
  58. /* Some good seeds - if the user does not specify one, one of these gets
  59.    chosen randomly. */
  60. int seeds[] =
  61.     {
  62.     0x0001, 0x0002, 0x0101, 0x0666, 0x1111, 0x1212, 0x1249, 0x2222,
  63.     0x3333, 0x4001, 0x4444, 0x5252, 0x5555, 0x6666, 0x8001, 0x8010
  64.     };
  65.  
  66. void Usage ()
  67. {
  68.     fprintf (stderr, "usage:  %s [-options ...]\n\n", ProgramName);
  69.     fprintf (stderr, "where options include:\n");
  70.     fprintf (stderr, "    -display host:dpy        X server to use\n");
  71.     fprintf (stderr, "    -geometry geom           size of window\n");
  72.     fprintf (stderr, "    -r                       use root window\n");
  73.     fprintf (stderr, "    -s seed                  random seed value\n");
  74.     fprintf (stderr, "    -v                       verbose mode\n");
  75.     fprintf (stderr, "    -q                       quiet mode\n");
  76.     fprintf (stderr, "\n");
  77.     exit (1);
  78. }
  79.  
  80. main(argc, argv)
  81. int argc;
  82. char **argv;
  83.     {
  84.     char **ap;
  85.     char *display = NULL;
  86.     char *geom = NULL;
  87.     int useRoot = 0;
  88.     int seed = 0;
  89.     Window win;
  90.     int winX, winY, winW, winH;
  91.     XSetWindowAttributes xswa;
  92.     Display *dpy;
  93.     Screen *scr;
  94.     GC gc;
  95.     XGCValues gcv;
  96.     XEvent xev;
  97. #define BATCHSIZE 400
  98.     XPoint points[BATCHSIZE];
  99.     int size, n;
  100.     long nmask; /* Was int. POHC 90/10/09 */
  101.     register int acc, i, x, y;
  102.     int xoffset, yoffset;
  103.  
  104.     ProgramName = argv[0];
  105.  
  106.     /* Process arguments: */
  107.     ap = argv;
  108.     while (*++ap) {
  109.         if (!strncmp(*ap, "-d", 2)) {
  110.             if (*++ap) {
  111.                 display = *ap;
  112.             } else 
  113.                 Usage ();
  114.         } else if (!strncmp(*ap, "-g", 2)) {
  115.             if (*++ap) {
  116.                 geom = *ap;
  117.             } else 
  118.                 Usage ();
  119.         } else if (**ap == '=')         /* obsolete */
  120.             geom = *ap;
  121.         else if (!strcmp(*ap, "-v"))
  122.             verbose = True;
  123.         else if (!strcmp(*ap, "-q"))
  124.             verbose = False;
  125.         else if (!strcmp(*ap, "-r"))
  126.             useRoot = 1;
  127.         else if (!strcmp(*ap, "-s")) {
  128.             if ( *++ap ) {
  129.                 char *fmt = "%d";
  130.                 char *cp = *ap;
  131.  
  132.                 if (*cp == '0') cp++;
  133.                 if (*cp == 'x' || *cp == 'X') fmt = "%x", cp++;
  134.                 if (sscanf (cp, fmt, &seed) != 1) 
  135.                     Usage ();
  136.             }  else
  137.                 Usage ();
  138.         } else
  139.             Usage ();
  140.     }
  141.  
  142.     if (!(dpy= XOpenDisplay(display)))
  143.             {
  144.         perror("Cannot open display\n");
  145.         exit(-1);
  146.             }
  147.  
  148.     scr = DefaultScreenOfDisplay(dpy);
  149.  
  150.     /* Set up window parameters, create and map window if necessary: */
  151.     if (useRoot)
  152.         {
  153.         win = DefaultRootWindow(dpy);
  154.         winX = 0;
  155.         winY = 0;
  156.         winW = DisplayWidth(dpy, DefaultScreen(dpy));
  157.         winH = DisplayHeight(dpy, DefaultScreen(dpy));
  158.         }
  159.     else
  160.         {
  161.         winW = 256;
  162.         winH = 256;
  163.         winX = (WidthOfScreen(scr) - winW) >> 1;
  164.         winY = (HeightOfScreen(scr) - winH) >> 1;
  165.         if (geom) 
  166.             XParseGeometry(geom, &winX, &winY,
  167.                        (unsigned int *)&winW,
  168.                        (unsigned int *)&winH);
  169.  
  170.         xswa.event_mask = 0;
  171.         xswa.background_pixel = BlackPixelOfScreen(scr);
  172.         win = XCreateWindow(dpy, RootWindowOfScreen(scr),
  173.             winX, winY, winW, winH, 0, 
  174.             DefaultDepthOfScreen(scr), InputOutput,
  175.             DefaultVisualOfScreen(scr),
  176.             CWEventMask | CWBackPixel, &xswa);
  177.         XChangeProperty(dpy, win, XA_WM_NAME, XA_STRING, 8, 
  178.                 PropModeReplace,
  179.                 (unsigned char *)"Muncher", 7);
  180.         XMapWindow(dpy, win);
  181.         }
  182.  
  183.     /* Set up a graphics context: */
  184.     gcv.foreground = (BlackPixelOfScreen(scr) ^ WhitePixelOfScreen(scr));
  185.     gcv.function = GXxor;
  186.     gc = XCreateGC(dpy, win, GCForeground | GCFunction, &gcv);
  187.  
  188.     /* Initialize munch algorithm. */
  189.     size = ( winW < winH ? winW : winH );
  190.     if ( size <= 0 ) size = 1;
  191.     for ( n = 30, nmask = 0x40000000; n >= 0; n--, nmask >>= 1 )
  192.         if ( size & nmask )
  193.             break;
  194.     size = 1 << n;
  195.     nmask = size - 1;
  196.     xoffset = ( winW - size ) / 2;
  197.     yoffset = ( winH - size ) / 2;
  198.     if ( seed == 0 )
  199.         {
  200. #if defined(SYSV) || defined(MSDOS) /* 90/06/26 POHC */
  201.         int tt; /* added this var for MSC6 time() JDC 91/08/08 */
  202.         srand((int) time(0) % 231);  /* RATIONAL 92/05/92 */
  203.         seed = seeds[rand() % (sizeof(seeds)/sizeof(seeds[0]) )];
  204. #else
  205.         srandom((int) time(0) % 231);
  206.         seed = seeds[random() % (sizeof(seeds)/sizeof(seeds[0]) )];
  207. #endif
  208.         }
  209.     if (verbose)
  210.         printf( "size = %d, seed = 0x%x\n", size, seed );
  211.     acc = 0;
  212.  
  213.     /* Loop forever computing and drawing batches of points. */
  214.     for (;;)
  215.         {
  216.         if (XPending(dpy))
  217.             XNextEvent(dpy, &xev);
  218.         
  219.         for ( i=0; i < BATCHSIZE; i++ )
  220.             {
  221.             x = acc & nmask;
  222.             y = ( ( acc >> n ) & nmask ) ^ x;
  223.  
  224.             points[i].x = x + xoffset;
  225.             points[i].y = y + yoffset;
  226.  
  227.             acc += seed;
  228.             }
  229.  
  230.         XDrawPoints(dpy, win, gc, points, BATCHSIZE, CoordModeOrigin);
  231.         XSync(dpy, 0);
  232.         }
  233.     }
  234.